package com.rstrulak.managedBeans.controllers;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.servlet.ServletException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.dao.NoteDao;
import com.rstrulak.model.Note;
import com.rstrulak.utils.Constants;
import com.rstrulak.utils.JSFUtils;

@Component("noteListBean")
@Scope("request")
public class NoteListBean implements Serializable {

	private static final long serialVersionUID = -3564509789239531008L;

	@Autowired
	private NoteDao noteDao;

	private List<Note> noteList = null;

	@PostConstruct
	public void init() {
		JSFUtils.reloadFlash();
		getNoteListFromDB();
	}

	private void getNoteListFromDB() {
		noteList = noteDao.getNoteListByUser(JSFUtils.getLoggedUser());
	}

	public List<Note> getNoteList() {
		return noteList;
	}

	public void setNoteList(List<Note> noteList) {
		this.noteList = noteList;
	}

	public void showNoteDetails(Long noteId) throws ServletException, IOException {
		JSFUtils.flashScope().put(Constants.NOTE_ID, noteId);
		JSFUtils.redirect("/protected/noteDetails.xhtml");
	}

	public void goToCreateNoteSite() throws ServletException, IOException {
		JSFUtils.redirect("/protected/newNote.xhtml");
	}
	
	public void goToCreateTask(Long noteId) throws ServletException, IOException{
		JSFUtils.flashScope().put(Constants.NOTE_ID, noteId);
		JSFUtils.redirect("/protected/newTaskFromNote.xhtml");
	}
	
	public void goToCreateProject(Long noteId) throws ServletException, IOException{
		JSFUtils.flashScope().put(Constants.NOTE_ID, noteId);
		JSFUtils.redirect("/protected/newProjectFromNote.xhtml");
	}

	public void deleteNote(Note note) throws ServletException, IOException {
		noteDao.delete(note);
		JSFUtils.redirect("/protected/note.xhtml");

	}

}
